package com.mp.chart.formatter;

import java.text.DecimalFormat;


public class DataFormatter {
    public static String getFormat(float value, String format) {
        if (Float.isNaN(value)) {
            return "";
        }
        DecimalFormat f = new DecimalFormat(format);
        return f.format(value);
    }

    public static String getFormat(float value) {
        return getFormat(value, "#0.00");
    }

    public static String getVolFormat(float value) {
        if (value > Math.pow(10, 8)) {
            return getFormat(value / (float) Math.pow(10, 8), "#0.00") + "亿股";
        } else if (value > 10000) {
            return getFormat(value / 10000.0f, "#0.00") + "万股";
        } else {
            return getFormat(value, "#0") + "股";
        }
    }

    public static String getFormatWithPre(float value, String format) {
        String pre = "";
        if (value > 0) {
            pre = "+";
        }
        return pre + getFormat(value, format);

    }


    public static String colorToString(int color) {
        int r = (color >> 16) & 0xFF;
        int g = (color >> 8) & 0xFF;
        int b = (color >> 0) & 0xFF;


        String red = Integer.toHexString(r);
        red = red.length() == 1 ? "0" + red : red;

        String green = Integer.toHexString(g);
        green = green.length() == 1 ? "0" + green : green;

        String blue = Integer.toHexString(b);
        blue = blue.length() == 1 ? "0" + blue : blue;


        return String.format("#%s%s%s", red,
                green,
                blue);
    }

    public static String getVolJishu(float num) {

        int e = (int) Math.floor(Math.log10(num));

        if (e >= 8) {
            return "亿股";
        } else if (e >= 4) {
            return "万股";
        } else {
            return "股";
        }


    }


}
